/*
 *  xfelp-window.c - This file is part of Xfelp
 *
 *  Copyright (C) 2008 Daniel Morales <daniel@daniel.com.uy>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include <webkit/webkitwebview.h>

#include "xfelp-main.h"
#include "xfelp-window.h"
#include "xfelp-browser.h"
#include "xfelp-xfcedoc.h"

static void xfelp_window_class_init        (XfelpWindowClass  *klass);
static void xfelp_window_init              (XfelpWindow       *window);
static void xfelp_window_finalize          (GObject           *object);

static void xfelp_window_action_print      (GtkAction         *action,
                                            XfelpWindow       *window);

static void xfelp_window_action_go_back    (GtkAction         *action,
                                            XfelpWindow       *window);

static void xfelp_window_action_go_forw    (GtkAction         *action,
                                            XfelpWindow       *window);

static void xfelp_window_action_go_home    (GtkAction         *action,
                                            XfelpWindow       *window);

static void xfelp_window_action_zoomout    (GtkAction         *action,
                                            XfelpWindow       *window);

static void xfelp_window_action_zoomin     (GtkAction         *action,
                                            XfelpWindow       *window);

static void xfelp_window_action_zoomnormal (GtkAction         *action,
                                            XfelpWindow       *window);

struct _XfelpWindowClass
{
  GtkWindowClass __parent__;
};

struct _XfelpWindow
{
  GtkWindow __parent__;

  GtkWidget      *view;
  GtkWidget      *statusbar;

  GtkActionGroup *action_group;
  GtkUIManager   *ui_manager;
};

static const GtkActionEntry window_actions[] =
{
  /* name, stock id, label, accelerator, tooltip, callback */
  { "file-menu", NULL, N_("_File"), NULL, },
  { "quit", GTK_STOCK_QUIT, NULL, NULL, NULL, NULL, },
  { "view-menu", NULL, N_("_View"), NULL, },
  { "help-menu", NULL, N_("_Help"), NULL, },
  { "about", GTK_STOCK_ABOUT, NULL, NULL, NULL, NULL, },

  { "go-back", GTK_STOCK_GO_BACK, NULL, NULL, NULL, G_CALLBACK (xfelp_window_action_go_back), },
  { "go-forward", GTK_STOCK_GO_FORWARD, NULL, NULL, NULL, G_CALLBACK (xfelp_window_action_go_forw), },
  { "go-home", GTK_STOCK_HOME, NULL, NULL, NULL, G_CALLBACK (xfelp_window_action_go_home), },

  { "print", GTK_STOCK_PRINT, NULL, NULL, NULL, G_CALLBACK (xfelp_window_action_print), },

  { "zoom-in", GTK_STOCK_ZOOM_IN, NULL, NULL, NULL, G_CALLBACK (xfelp_window_action_zoomin), },
  { "zoom-out", GTK_STOCK_ZOOM_OUT, NULL, NULL, NULL, G_CALLBACK (xfelp_window_action_zoomout), },
  { "zoom-normal", GTK_STOCK_ZOOM_100, NULL, NULL, NULL, G_CALLBACK (xfelp_window_action_zoomnormal), },
};

static const gchar *window_ui_info =
"<ui>"
"  <menubar name='main-menubar'>"
"    <menu action='file-menu'>"
"      <menuitem action='print'/>"
"      <separator/>"
"      <menuitem action='quit'/>"
"    </menu>"
"    <menu action='view-menu'>"
"      <menuitem action='zoom-in'/>"
"      <menuitem action='zoom-out'/>"
"      <menuitem action='zoom-normal'/>"
"    </menu>"
"    <menu action='help-menu'>"
"      <menuitem action='about'/>"
"    </menu>"
"  </menubar>"
"  <toolbar action='main-toolbar'>"
"    <toolitem action='go-back'/>"
"    <toolitem action='go-forward'/>"
"    <toolitem action='go-home'/>"
"    <separator/>"
"    <toolitem action='zoom-in'/>"
"    <toolitem action='zoom-out'/>"
" </toolbar>"
"</ui>";

static GObjectClass *xfelp_window_parent_class;

GType
xfelp_window_get_type (void)
{
  static GType type = G_TYPE_INVALID;

  if (G_UNLIKELY (type == G_TYPE_INVALID))
  {
    static const GTypeInfo info =
    {
      sizeof (XfelpWindowClass),
      NULL,
      NULL,
      (GClassInitFunc) xfelp_window_class_init,
      NULL,
      NULL,
      sizeof (XfelpWindow),
      0,
      (GInstanceInitFunc) xfelp_window_init,
      NULL,
    };

    type = g_type_register_static (GTK_TYPE_WINDOW, "XfelpWindow", &info, 0);
  }

  return type;
}

static void
xfelp_window_class_init (XfelpWindowClass *klass)
{
  GObjectClass   *gobject_class;

  xfelp_window_parent_class = g_type_class_peek_parent (klass);

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = xfelp_window_finalize;
}

static void
xfelp_window_init (XfelpWindow *window)
{
  GtkAccelGroup *accel_group;
  GtkWidget     *scroll;
  GtkWidget     *vbox;
  GtkWidget     *bar;

  /* Setup main window */
  g_signal_connect (G_OBJECT (window), "destroy", gtk_main_quit, NULL);
  gtk_window_set_title (GTK_WINDOW (window), "Xfelp");
  gtk_window_set_default_size (GTK_WINDOW (window), XFELP_DEFAULT_WIDTH, XFELP_DEFAULT_HEIGHT);
  gtk_window_move (GTK_WINDOW (window), 0, 0);

  /* Main container */
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  /* UI */
  window->action_group = gtk_action_group_new ("XfelpWindow");
  gtk_action_group_set_translation_domain (window->action_group, GETTEXT_PACKAGE);
  gtk_action_group_add_actions (window->action_group, window_actions, G_N_ELEMENTS (window_actions), GTK_WIDGET (window));

  window->ui_manager = gtk_ui_manager_new ();
  gtk_ui_manager_insert_action_group (window->ui_manager, window->action_group, 0);
  gtk_ui_manager_add_ui_from_string (window->ui_manager, window_ui_info, -1, NULL);

  accel_group = gtk_ui_manager_get_accel_group (window->ui_manager);
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

  bar = gtk_ui_manager_get_widget (window->ui_manager, "/main-menubar");
  gtk_box_pack_start (GTK_BOX (vbox), bar, FALSE, TRUE, 0);

  bar = gtk_ui_manager_get_widget (window->ui_manager, "/main-toolbar");
  gtk_box_pack_start (GTK_BOX (vbox), bar, FALSE, TRUE, 0);

  /* Render scroll */
  scroll = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (vbox), scroll, TRUE, TRUE, 0);
  
  /* Setup the webkit renderer */
  window->view = xfelp_browser_new ();
  gtk_container_add (GTK_CONTAINER (scroll), window->view);

  /* Statusbar */
  window->statusbar = gtk_statusbar_new ();
  gtk_box_pack_start (GTK_BOX (vbox), window->statusbar, FALSE, TRUE, 0);

  gtk_widget_show_all (GTK_WIDGET (window));

  /* Load homepage */
  xfelp_window_action_go_home (NULL, window);
}

static void
xfelp_window_finalize (GObject *object)
{
  XfelpWindow *window = XFELP_WINDOW (object);

  /* Cleanup... */
  g_object_unref (G_OBJECT (window->ui_manager));
  g_object_unref (G_OBJECT (window->action_group));

  (*G_OBJECT_CLASS (xfelp_window_parent_class)->finalize) (object);
}

static void
xfelp_window_action_print (GtkAction    *action,
                           XfelpWindow  *window)
{
  /* Print current page */
  webkit_web_view_execute_script (WEBKIT_WEB_VIEW (window->view), "print ();");
}

static void
xfelp_window_action_zoomout (GtkAction   *action,
                             XfelpWindow *window)
{
  /* Increase the Zoom view */
  webkit_web_view_zoom_out (WEBKIT_WEB_VIEW (window->view));
}

static void
xfelp_window_action_zoomin  (GtkAction   *action,
                             XfelpWindow *window)
{
  /* Decrease the Zoom view */
  webkit_web_view_zoom_in (WEBKIT_WEB_VIEW (window->view));
}

static void
xfelp_window_action_zoomnormal (GtkAction   *action,
                                XfelpWindow *window)
{
  /* Set normal zoom */
  webkit_web_view_set_zoom_level (WEBKIT_WEB_VIEW (window->view), 1.0);
}

static void
xfelp_window_action_go_back (GtkAction   *action,
                             XfelpWindow *window)
{
  /* Open previous page */
  webkit_web_view_go_back (WEBKIT_WEB_VIEW (window->view));
}

static void
xfelp_window_action_go_forw (GtkAction   *action,
                             XfelpWindow *window)
{
  /* Open next page */
  webkit_web_view_go_forward (WEBKIT_WEB_VIEW (window->view));
}

static void
xfelp_window_action_go_home (GtkAction   *action,
                             XfelpWindow *window)
{
  gchar *homepage;

  /* Loads the home page */
  homepage = xfelp_xfcedoc_get_home ();

  if (G_LIKELY (homepage != NULL))
    webkit_web_view_open (WEBKIT_WEB_VIEW (window->view), homepage);

  g_free (homepage);
}

XfelpWindow *
xfelp_window_get_default (void)
{
  static XfelpWindow *window = NULL;

  if (G_UNLIKELY (window == NULL))
  {
    /* allocate a new instance */
    window = g_object_new (XFELP_TYPE_WINDOW, NULL);
    g_object_add_weak_pointer (G_OBJECT (window), (gpointer) &window);
  }
  else
  {
    /* take a reference */
    g_object_ref (G_OBJECT (window));
  }

  return window;
}

void
xfelp_window_set_title (XfelpWindow *window,
                        const gchar *title)
{
  gchar *newtitle = g_strdup_printf (_("Xfelp - %s"), title);

  gtk_window_set_title (GTK_WINDOW (window), newtitle);

  g_free (newtitle);
}
